package zzhua.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import zzhua.entity.Student;

import java.util.Collection;

@RestController
@RequestMapping("/myRest")
public class MyRestController {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("findAll")
    public Collection<Student> findAll(){
        return restTemplate.getForEntity("http://localhost:8010/student/findAll", Collection.class).getBody();
    }

    @GetMapping("findById/{id}")
    public Student findById(@PathVariable(name = "id") Integer id) {
        return restTemplate.getForEntity("http://localhost:8010/student/findById/{id}", Student.class, id).getBody();
    }

    @PostMapping("add")
    public String add(@RequestBody Student student){
        restTemplate.postForEntity("http://localhost:8010/student/add", student,Void.class);
        return "ok";
    }

    @PutMapping("updateById")
    public String updateById(@RequestBody Student student){
        restTemplate.put("http://localhost:8010/student/updateById",student,Void.class);
        return "ok";
    }

    @DeleteMapping("deleteById/{id}")
    public String deleteById(@PathVariable(name = "id") Integer id){
        restTemplate.delete("http://localhost:8010/student/deleteById/{id}",id);
        return "ok";
    }
}
